<html>
<head><meta charset="utf-8"><title>impl trait in closure argument · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html">impl trait in closure argument</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220984517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220984517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220984517">(Dec 26 2020 at 23:53)</a>:</h4>
<p>Why do we not support impl traits in closure arguments? </p>
<p>eg: </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">g</span><span class="p">(</span><span class="n">x</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Debug</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Debug</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">//               ^^^^^^^^^  `impl Trait` not allowed outside of function and inherent method return types</span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="n">c</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The way I understand it, closures are functions with an environment, so I'm wondering if this just needs to be worked into the compiler, or are there reasons to not do so?</p>



<a name="220984571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220984571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220984571">(Dec 26 2020 at 23:55)</a>:</h4>
<p>Actually, now that I think of this, the error message is also a little incorrect, should it not say something along the lines of <code>impl Trait</code> is not allowed outside of function args and return types because the following is valid Rust code</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Debug</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220985133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220985133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220985133">(Dec 27 2020 at 00:12)</a>:</h4>
<p>Okay so this was postponed: <a href="https://github.com/rust-lang/rfcs/pull/1650">https://github.com/rust-lang/rfcs/pull/1650</a></p>



<a name="220985260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220985260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220985260">(Dec 27 2020 at 00:17)</a>:</h4>
<p>In the meantime, given your use case, you can use <code>&amp;dyn Debug</code> for your parameter</p>



<a name="220985357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220985357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220985357">(Dec 27 2020 at 00:20)</a>:</h4>
<p>Or if you want to go the extra mile, see the end of this post: <a href="https://users.rust-lang.org/t/generic-closure/47732/5">https://users.rust-lang.org/t/generic-closure/47732/5</a></p>



<a name="220986728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220986728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220986728">(Dec 27 2020 at 01:07)</a>:</h4>
<p>I don't think closures normally support declaring new generic args and we probably didn't want a case where impl trait is the only way to write something</p>



<a name="220986744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220986744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220986744">(Dec 27 2020 at 01:07)</a>:</h4>
<p>Not sure if closures as something that can be monomorphized is also well supported</p>



<a name="220986866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220986866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220986866">(Dec 27 2020 at 01:11)</a>:</h4>
<p>I think this is one instance of a general hole in expressivity involving the lack of <code>decltype</code> (not that I like that approach to naming the unnameable)</p>



<a name="220986919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220986919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220986919">(Dec 27 2020 at 01:13)</a>:</h4>
<p>ideally you would be able to name the type of x inside the body (without changing the signature of the function), and then use that type in the declaration of <code>c</code></p>



<a name="220986968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220986968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220986968">(Dec 27 2020 at 01:14)</a>:</h4>
<p>(assuming that the goal here is to declare a non-generic closure <code>c</code> specialized only to the particular type of <code>x</code>, not a generic closure as in the links)</p>



<a name="220987219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220987219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220987219">(Dec 27 2020 at 01:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/impl.20trait.20in.20closure.20argument/near/220986968">said</a>:</p>
<blockquote>
<p>(assuming that the goal here is to declare a non-generic closure <code>c</code> specialized only to the particular type of <code>x</code>, not a generic closure as in the links)</p>
</blockquote>
<p>yea that's kind of what I'm trying to do, and I really just need it for one particular type and. I kind of don't want to add another function just for it</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span> <span class="nf">trait_func</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span>: <span class="nc">A</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="n">x</span><span class="p">.</span><span class="n">trait_func</span><span class="p">()</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>

<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">c</span><span class="p">(</span><span class="n">X</span>::<span class="n">new</span><span class="p">());</span><span class="w"> </span><span class="c1">// where X impls A</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220988032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220988032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220988032">(Dec 27 2020 at 01:47)</a>:</h4>
<p>you just used a trait where a type is expected? Is this <code>impl</code> or <code>dyn</code></p>



<a name="220988089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220988089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220988089">(Dec 27 2020 at 01:48)</a>:</h4>
<p>At least in these examples, it's not really a closure at all (it doesn't capture anything), so you can use a local function instead</p>



<a name="220988149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220988149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220988149">(Dec 27 2020 at 01:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/impl.20trait.20in.20closure.20argument/near/220988032">said</a>:</p>
<blockquote>
<p>you just used a trait where a type is expected? Is this <code>impl</code> or <code>dyn</code></p>
</blockquote>
<p><code>x: impl A</code> didn't work, and yeah I have been hiding the capture part <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span>. <code>x: &amp;dyn A</code> errors out with trait_func method cannot be invoked on a trait object. I haven't gotten too much chance to debug it yet</p>



<a name="220988212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220988212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220988212">(Dec 27 2020 at 01:52)</a>:</h4>
<p>I would assume <code>x: A</code> would get interpreted as <code>x: dyn A</code> and so would get an unsized local error</p>



<a name="220988270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220988270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220988270">(Dec 27 2020 at 01:54)</a>:</h4>
<p>the other workaround would be to use <code>fn g&lt;T: Debug&gt;(x: T)</code> for the outer function, of course</p>



<a name="220988327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/220988327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#220988327">(Dec 27 2020 at 01:56)</a>:</h4>
<p>yeah I'm kind of avoiding the outer function,  especially since I don't care about other types that implement that trait</p>



<a name="221005293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/221005293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#221005293">(Dec 27 2020 at 11:32)</a>:</h4>
<p>The following works <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// Situation:</span>
<span class="k">trait</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">fn</span> <span class="nf">method</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">f</span><span class="w"> </span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">struct</span> <span class="nc">X</span><span class="p">;</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="c1">// and we want to write a closure that takes a `f()` as a parameter</span>

<span class="kd">let</span><span class="w"> </span><span class="n">captured</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"> </span><span class="c1">// let's imagine an `fn` is out of the question</span>
<span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">captured</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">{</span><span class="n">x</span><span class="p">}</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f</span><span class="p">();</span><span class="w"> </span><span class="k">loop</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// typeof(x) = typeof(f())</span>
<span class="w">    </span><span class="n">x</span><span class="p">.</span><span class="n">method</span><span class="p">();</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="n">c</span><span class="p">(</span><span class="n">f</span><span class="p">());</span><span class="w"></span>
</code></pre></div>
<p>Note that in the <code>if false</code> branch, should <code>f</code> be taking parameters, each can be replaced with <code>loop {}</code></p>



<a name="221025700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/221025700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#221025700">(Dec 27 2020 at 22:19)</a>:</h4>
<p>That's kinda smart. That said I realise impl trait would have not solved my problem. I really just need to be able to write </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="o">|</span><span class="n">T</span>: <span class="nc">TraitBound</span><span class="o">|</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w">  </span><span class="p">{</span><span class="w"></span>
<span class="c1">// ...</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221026548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20in%20closure%20argument/near/221026548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20in.20closure.20argument.html#221026548">(Dec 27 2020 at 22:45)</a>:</h4>
<p>But if <code>T</code> is coming from an outer scope, then it should already have the trait bound from there</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>